package im.starchat.chat.activity.profile;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.Arrays;

import butterknife.ButterKnife;
import butterknife.InjectView;
import im.starchat.chat.R;
import im.starchat.chat.XLApplication;
import im.starchat.chat.adapter.ContributionAdapter;
import im.starchat.chat.rest.RestClient;
import im.starchat.chat.rest.model.ContributionBean;
import im.starchat.chat.rest.model.api.ContributionResponse;
import im.starchat.chat.utils.GsonUtil;
import im.starchat.chat.utils.Util;
import im.starchat.chat.view.swipeRefreshLayout.SwipeRefreshLayout;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;


/**
 * 人气贡献榜
 */
public class ContributionActivity extends Activity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener{

    @InjectView(R.id.contribution_back)
    ImageView mBackBtn;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.swipe_refresh_layout_list)
    SwipeRefreshLayout mSwipeRefreshLayout;

    @InjectView(R.id.popular_count)
    TextView mPopularCount;

    private ContributionAdapter mContributionAdapter;

    private LinearLayoutManager mLayoutManager;

    public static final String FLAG_USER_ID = "user_id";

    private String mUserId = "";

    private int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contribution);
        ButterKnife.inject(this);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);

        initIntent();

        mBackBtn.setOnClickListener(this);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mContributionAdapter = new ContributionAdapter(this);
        mRecyclerView.setAdapter(mContributionAdapter);
        getContributionList();
        mSwipeRefreshLayout.setRefreshing(true);

    }

    private void initIntent(){
        Intent intent = getIntent();
        if(intent != null){
            mUserId = intent.getStringExtra(FLAG_USER_ID);
        }
    }

    @Override
    public void onRefresh() {
        mCurrentPage = RestClient.DEFAULT_PAGE;
        getContributionList();
    }

    @Override
    public void onLoad() {
        if(mHasMoreData){
            mCurrentPage ++;
            getContributionList();
        }else{
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            Toast.makeText(ContributionActivity.this, "没有更多数据", Toast.LENGTH_SHORT).show();
        }
    }



    private void getContributionList(){
        if(!TextUtils.isEmpty(mUserId)){
            XLApplication.getInstance().getRestClient().getContributions(mUserId, mCurrentPage, RestClient.DEFAULT_PREPAGE, new Callback<ContributionBean[]>() {
                @Override
                public void success(ContributionBean[] userBean, Response response) {
                    try {
                        ContributionResponse contributionResponse = GsonUtil.fromJson(Util.inputStream2String(response.getBody().in()), ContributionResponse.class);
                        mPopularCount.setText(contributionResponse.getTotal_popularity() + "人气值");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {

                    }

                    if (mCurrentPage == RestClient.DEFAULT_PAGE) {
                        mContributionAdapter.refreshData(Arrays.asList(userBean));
                    } else {
                        mContributionAdapter.addAll(Arrays.asList(userBean));
                    }
                    if (userBean.length < 20) {
                        mHasMoreData = false;
                    }else{
                        mHasMoreData = true;
                    }
                    mSwipeRefreshLayout.setRefreshing(false);
                    mSwipeRefreshLayout.setLoading(false);
                }

                @Override
                public void failure(RetrofitError error) {
                    mSwipeRefreshLayout.setRefreshing(false);
                    mSwipeRefreshLayout.setLoading(false);
                }
            });
        }
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.contribution_back:
                finish();
                break;
        }
    }
}
